package com.bajiao.admin.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bajiao.admin.base.AdminBaseController;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.aspect.annotation.AutoLog;
import com.bajiao.entity.config.Config;
import com.bajiao.service.config.ConfigService;
import com.bajiao.vo.config.ConfigVO;
import com.bajiao.vo.config.UpdateConfigGroupVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 系统配置
 * @Author: FanJq
 * @Date: 2024-10-10
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "系统配置")
@RestController
@RequestMapping("/config/config")
public class ConfigController extends AdminBaseController<Config, ConfigService> {

    @Autowired
    private ConfigService configServiceMove;

    @AutoLog(value = "系统配置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(Config systemConfig,
                                               @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                               @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                               HttpServletRequest req) {
        Result<IPage<Config>> result = new Result<IPage<Config>>();
        LambdaQueryWrapper<Config> queryWrapper = Wrappers.<Config>lambdaQuery()
                .like(StringUtils.isNotBlank(systemConfig.getConfigName()),Config::getConfigName,systemConfig.getConfigName())
                .like(StringUtils.isNotBlank(systemConfig.getConfigGroup()),Config::getConfigGroup,systemConfig.getConfigGroup())
                .like(StringUtils.isNotBlank(systemConfig.getTitle()),Config::getTitle,systemConfig.getTitle())
                .ne(Config::getType,6)
                .orderByDesc(Config::getCreateTime);
        startPage();
        return Result.ok(getDataTable(configServiceMove.list(queryWrapper)));
    }

    /**
     *   添加
     * @param systemConfig
     * @return
     */
    @AutoLog(value = "系统配置-添加")
    @PostMapping(value = "/add")
    public Result<Config> add(@RequestBody Config systemConfig) {
        Result<Config> result = new Result<Config>();
        try {
            configServiceMove.save(systemConfig);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     *  编辑
     * @param systemConfig
     * @return
     */
    @AutoLog(value = "系统配置-编辑")
    @PutMapping(value = "/edit")
    public Result<Config> edit(@RequestBody Config systemConfig) {
        Result<Config> result = new Result<Config>();
        Config systemConfigEntity = configServiceMove.getById(systemConfig.getId());
        if(systemConfigEntity==null) {
            result.error500("未找到对应实体");
        }else {
            boolean ok = configServiceMove.updateById(systemConfig);
            //TODO 返回false说明什么？
            if(ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     *   通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "系统配置-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<Config> delete(@RequestParam(name="id",required=true) String id) {
        Result<Config> result = new Result<Config>();
        Config systemConfig = configServiceMove.getById(id);
        if(systemConfig==null) {
            result.error500("未找到对应实体");
        }else {
            boolean ok = configServiceMove.removeById(id);
            if(ok) {
                result.success("删除成功!");
            }
        }

        return result;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @AutoLog(value = "系统配置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<Config> queryById(@RequestParam(name="id",required=true) String id) {
        Result<Config> result = new Result<Config>();
        Config systemConfig = configServiceMove.getById(id);
        if(systemConfig==null) {
            result.error500("未找到对应实体");
        }else {
            result.setResult(systemConfig);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 修改多个配置
     *
     * @param configList
     * @return
     */
    @PostMapping("/update-values")
    public Result getValueByNames(@RequestBody List<ConfigVO> configList) {
        configList.forEach(e -> {
            configServiceMove.updateByConfigGroupAndConfigName(e.getConfigGroup(), e.getConfigName(), e.getConfigValue(),e.getTitle());
        });
        return Result.ok("操作成功");
    }

    /**
     * 根据分组查询系统配置
     */
    @GetMapping(value = "/queryByConfigGroup")
    public Result queryByConfigGroup(@RequestParam String configGroup) {
        return Result.ok(configServiceMove.queryNameAndValueByGroup(configGroup));
    }


    /**
     * 修改组数据
     */
    @PutMapping(value = "/updateByConfigGroup")
    public Result updateByConfigGroup(@RequestBody UpdateConfigGroupVO updateConfigGroup) {
        String configGroup = updateConfigGroup.getConfigGroup();
        updateConfigGroup.getConfigList().forEach(e -> {
            configServiceMove.updateByConfigGroupAndConfigName(configGroup, e.getConfigName(), e.getConfigValue(),e.getTitle());
        });
        return Result.ok();
    }

    /**
     * 修改组数据
     */
    @PutMapping(value = "/updateExperienceById")
    public Result updateExperienceById(@RequestBody Config config) {
        Config con = configServiceMove.getById(config.getId());
        con.setTitle(config.getTitle());
        con.setDescription(config.getDescription());
        con.setConfigValue(config.getConfigValue());
        configServiceMove.updateById(con);
        return Result.ok();
    }
}
